local skynet = require "skynet"
local cluster = require "skynet.cluster"
local queue = require "skynet.queue"
local routerbase = require "routerbase"
local filelog = require "filelog"
local filename = "table.lua"
local cs = queue()

require "hbslenum"

local gametable = routerbase:new({
	table_data = {
		table_index = 0,
		table_heart_time = 0,
		is_deleting = false,
		seats = {}, --座位信息
		state = 0,
		sitdown_player_num = 0, --坐下的玩家数
		conf = nil,
		gamelogic = nil,
		svr_id = "",
		timer_id = -1,
		notice_robot_timer_id = -1,
		check_hbinfo_timer_id = -1,
		check_mlinfo_timer_id = -1,	---检查埋雷信息定时器
		isdelete = false, --是否被标示为可删除
		delete_table_timer_id = -1,
		delay_delete_timer_id = -1,
		action_to_time = 0,    --当前操作玩家的到期时间
		action_type = 0,       --玩家当前操作类型
		initCards = nil,	   --牌池
		iswilldelete = 0, 		---在游戏中如果收到删除指令,则置1,游戏结束再处理
		game_records = nil,		----记录战绩
		backofconf = nil,       ----缓存更新的配置文件
		cardrecords = nil, 		----	
		putcardsnums = 0,
		circlecount = 0,        ---- 当前圈数
		fanpaicards = {},
		wantbury_landmineinfo = {},
		cur_landmineinfo = {},
		already_qhb_playerinfo = {},
		one_start_time = 0,
		one_end_time = 0,
		tablerecords = nil,
		replaylogname = "",
		is_rematch = false,
		table_end_flag = false,
		already_bury_nums = 0,
		can_qiang_landmineinfos = {},   	----无限模式可以抢红包队列
		already_qiang_landmineinfos = {}, 	----
		all_unlimit_hb_timers = {},			----
		qhb_num_rank_lists = {},			----抢红包数量排行榜
		qhb_money_rank_lists = {}, 			----抢红包金额排行榜
		unlimit_check_hbover_timer = -1,
		add_already_qhb_infos = {},
		
	},
	logicpool = {},
})

function gametable:init()
	self:add_func("tablemsg/tablecmd")
	self:add_func("tablemsg/tablerequest")
	self:add_func("tablemsg/tabletimer")
	self:add_logic("tablelogic", "tablemsg/tablelogic")
	self:add_logic("seatlogic", "tablemsg/seatlogic")
	self:add_logic("tablerobothelper", "tablemsg/tablerobothelper")
	self:add_logic("gamelogic", "tablemsg/gamelogic")		---钻石场逻辑 	
end

function gametable:update_heart_time()
	self.table_data.table_heart_time = timetool.get_time()
end

function gametable:add_logic(name, logic)
	self.logicpool[name] = require(logic)
	if self.logicpool[name].init and type(self.logicpool[name].init) == "function" then
		self.logicpool[name].init(gametable)
	end
end

function gametable:get_logicbyname(name)
	return self.logicpool[name]
end

function gametable:send_notice_to_players(rids, msgname, message, retcode, retstr)
	cluster.send("gatenode", ".router", "notify", rids, "hbsl."..msgname, message, retcode, retstr)
end

function gametable:notice_tableinfo_togatenode(msgname, rid, gamenode, table_index)
	cluster.send("gatenode", ".router", msgname, rid, gamenode, table_index)
end

function gametable:send_notice_to_gatenode(msgname, message, comment, ...)
	cluster.send("gatenode", ".router", msgname, message, comment, ...)
end

function gametable:process(command, ...)
	skynet.call(skynet.self(), "lua", command, ...)
end

function gametable:updateplayernum()
	local robot_num = 0
	local seatlogic = self:get_logicbyname("seatlogic")
	for index, seat in ipairs(self.table_data.seats) do
		if not seatlogic.is_empty(seat) then
			if seat.playerinfo.level == 99 then
				robot_num = robot_num + 1
			end
		end
	end

	skynet.send(".router", "lua", "update_table_player", self.table_data.conf.table_index, self.table_data.conf.cur_player_num, 
			self.table_data.conf.table_type, self.table_data.conf.match_type, robot_num)
end

function gametable:exit_service()
	skynet.exit()
end

function gametable:start()
	skynet.dispatch("lua", 
		function (session, source, command, ...)
			local f = gametable.CMD[command]
			local status = true, result 
			if f == nil then
				filelog.sys_error(filename.." CMD[command] is nil : ", command, ...)
				result = {retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "request error"}
				skynet.retpack(result)
			else
				status, result = cs(skynet.pcall, f, ... )
				if not status then
					filelog.sys_error(filename.." CMD[command] skynet.pcall failed : ", command, result, ...)
				end
			end
		end)
	gametable:init()
end

skynet.start(function()
	gametable:start()
end)